<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Delivery extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'delivery';
        $this->load->model('Mdelivery');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 9, $this->session->userdata('admin_id'));
    }
    
    public function homeDelivery()
    {
        if($this->_per->read != 1 && $this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tiêu đề', 'required');
        $this->form_validation->set_rules('features_check', 'Ảnh bài viết', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        
        $this->data['data'] = $this->Mdelivery->getHomeDelivery();
        if($this->form_validation->run() === TRUE)
        {
            
            $dataimage = $this->getParamString('features_');//die($dataimage);
            
            $dataimage_check = $this->getParamString('features_check');
            $verify_image = $this->getParamString('features_verify');
            if(!in_array($verify_image, array(null, '')))
            {
                if($verify_image != $dataimage_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image);
                }
            }
            
            /*if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'home-delivery.jpg';
                $path = 'images/features/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }*/
            
            $path1 = 'images/features/';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$img = '';
			}else{
				$data_img = $this->upload->data();
				$img = $path1.$data_img['file_name'];
			}
            
            $title = $this->getParamString('title');
            $content = $this->getParamString('content');
            
            $this->data['data'] = $this->Mdelivery->homeDeliveryDb($title,$img,$content);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/static/home-delivery";</script>');
            }
        }
        $this->data['action'] = 'homedelivery';
        $this->load->view('template/layout', $this->data);
    }
}